package org.dreamfly.dreamalarm.task;

import org.dreamfly.dreamalarm.bean.Alarm;
import org.dreamfly.dreamalarm.inter.HandleComponent;
import org.dreamfly.dreamalarm.util.db.DataBaseHelper;

import android.content.Context;

import com.github.droidfu.concurrent.BetterAsyncTask;

public class HandleAsyncTask extends BetterAsyncTask<String, Void, String> {

	HandleComponent component;
	Alarm alarm;
	boolean turnChanged;
	
	long id=-1;

	public HandleAsyncTask(Context arg0, HandleComponent component,
			Alarm alarm, Boolean needDialog, boolean turnChanged) {
		super(arg0);
		// TODO Auto-generated constructor stub

		this.component = component;
		this.alarm = alarm;
		this.turnChanged=turnChanged;
		if (!needDialog) {
			super.disableDialog();
		}
	}

	// 处理修改内容
	@Override
	protected String doCheckedInBackground(Context context, String... obj)
			throws Exception {
		super.doCheckedInBackground(context, obj);

		id=DataBaseHelper.getInstance().saveOrUpdateAlarm(alarm);

		return "";
	}

	@Override
	protected void after(Context arg0, String arg1) {
		// TODO Auto-generated method stub
		if(id==-1){
			//update
			component.handleSuccessRefresh(false,alarm,turnChanged);
		}else{
			//insert
			alarm.setId((int)id);
			component.handleSuccessRefresh(true, alarm,turnChanged);
		}
		
	}

	@Override
	protected void handleError(Context arg0, Exception arg1) {
		// TODO Auto-generated method stub
		component.handleErrorRefresh();
	}

}
